package solutions.leetcode.normal

/**
 * @author lizhidong
 * <a href="https://leetcode.cn/problems/evaluate-the-bracket-pairs-of-a-string/">1807. 替换字符串中的括号内容</a>
 */
private class Solution1807 {
    fun evaluate(s: String, knowledge: List<List<String>>): String {
        val sb = StringBuilder()
        val map = knowledge.associate { it[0] to it[1] }
        var ind = 0
        while (ind < s.length) {
            val c = s[ind++]
            if (c == '(') {
                val key = StringBuilder()
                while (ind < s.length) {
                    val cc = s[ind++]
                    if (cc == ')') {
                        break
                    }
                    key.append(cc)
                }
                sb.append(map[key.toString()] ?: "?")
            } else {
                sb.append(c)
            }
        }
        return sb.toString()
    }
}

fun main() {
    println(Solution1807().evaluate(
        "(name)is(age)yearsold",
        arrayListOf(
            arrayListOf(
                "name","bob"
            )
        )
    ))
}